package logic

import (
	"context"
	"errors"
	"video-manager/model/mysql"

	"video-manager/usersrpc/internal/svc"
	"video-manager/usersrpc/usersrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadVideoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUploadVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadVideoLogic {
	return &UploadVideoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UploadVideoLogic) UploadVideo(in *usersrpc.UploadVideoRequest) (*usersrpc.UploadVideoResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.User

	if err := findUser.FindUserByUserId(mysql.DB, in.UserId); err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}

	video := &mysql.Video{
		//Model:          gorm.Model{},
		Upload_user_id: int(in.UserId),
		Video_title:    in.VideoTitle,
		Video_category: in.VideoCategory,
		Video_link:     in.VideoLink,
		Cover_link:     in.CoverLink,
		Video_status:   int(in.VideoStatus),
	}

	if err := video.UploadVideo(mysql.DB); err != nil {
		return nil, errors.New("视频上传失败")
	} else {
		in.VideoStatus = 0
	}
	return &usersrpc.UploadVideoResponse{
		VideoId: int64(video.ID),
	}, nil
}
